unit Unit4; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Buttons,
  StdCtrls, sqldb, Unit1;

type

  { TForm4 }

  TForm4 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    SQLQuery1: TSQLQuery;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    codigo : String;
  end; 

var
  Form4: TForm4; 

implementation

{ TForm4 }

procedure TForm4.FormShow(Sender: TObject);
begin
  Edit1.Text := '';
  Edit2.Text := '';
  Edit3.Text := '';
  Edit4.Text := '';
  Edit5.Text := '';
  
  if codigo <> '' then
  begin
    if SQLQuery1.Active = True then
      SQLQuery1.Close;

    SQLQuery1.DataBase := Form1.PQConnection1;
    SQLQuery1.Transaction := Form1.SQLTransaction1;
    SQLQuery1.SQL.Text := 'select * from proveedor where prv_cdg = ' + codigo;

    SQLQuery1.Open;
    
    while not SQLQuery1.EOF do
    begin
      Edit1.Text := SQLQuery1.Fields[1].AsString;
      Edit2.Text := SQLQuery1.Fields[2].AsString;
      Edit3.Text := SQLQuery1.Fields[3].AsString;
      Edit4.Text := SQLQuery1.Fields[4].AsString;
      Edit6.Text := SQLQuery1.Fields[5].AsString;
      SQLQuery1.Next;
    end;
    
    Edit5.Text := codigo;
    
    SQLQuery1.Close;
    
    Edit1.SetFocus;
  end
  else
    Close;
end;

procedure TForm4.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
end;

procedure TForm4.Button2Click(Sender: TObject);
begin
  Close;
end;

procedure TForm4.Button1Click(Sender: TObject);
var
  QueryString : String;

begin
  QueryString := 'update proveedor set prv_nombre = ''' + Edit1.Text + ''', prv_contacto = '''
    + Edit2.Text + ''', prv_email = ''' + Edit3.Text + ''', prv_telefono = ''' + Edit4.Text
    + ''', prv_direccion = ''' + Edit6.Text + '''  where prv_cdg = ' + Edit5.Text;
    
  Form1.PQConnection1.ExecuteDirect('begin work;');
  Form1.PQConnection1.ExecuteDirect(QueryString);
  Form1.PQConnection1.ExecuteDirect('commit work;');

  ShowMessage('MODIFICACIÓN EXITOSA');
  Close;
end;

initialization
  {$I unit4.lrs}

end.

